package memoryimage.provider;

import memoryimage.models.ByteDataArray;

import org.eclipse.jface.resource.ColorRegistry;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.viewers.ITableColorProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;

public class MemoryLabelProvider extends LabelProvider implements
	ITableLabelProvider, ITableColorProvider {
	private Display display;
	private Color[] pallet;
	private ColorRegistry colorRegistry;
	private ImageRegistry imageRegistry;
	private int dataByteWidth = 16;
	
	public MemoryLabelProvider(Display display) {
		this.display = display;
		
		pallet = new Color[256];
		int r = 0;
		int g = 0;
		int b = 0;
		for(int i = 0; i < 256; i++) {
			if(i == 0) {
				r = 255;
				g = 255;
				b = 255;
			} else if(i < 64) {
				r = 255 - (i * 4);
				g = 255;
				b = 0;
			} else if (i < 128) {
				r = 0;
				g = 255 - ((i - 64) * 4);
				b = 255;
			} else if (i < 192) {
				r = 128;
				g = 128;
				b = 255 - ((i - 128) * 4);
			} else {
				r = 255 - ((i - 192) * 4);
				g = 32;
				b = 32;
			}
			pallet[i] = new Color(display, r, g, b);
		}

		// Generate ColoerRegister to set RGB value of pallet[]
		colorRegistry = new ColorRegistry();
		for (Color color : pallet) {
			colorRegistry.put(color.toString(), color.getRGB());
		}
		
		// Generate ImageRegistry
		imageRegistry = new ImageRegistry();
	}
	

	private Image getByteDataArrayImage(ByteDataArray byteDataArray) {
		  Image result = imageRegistry.get(Integer.toString(byteDataArray.cells[0]));
		  
		  // If no image registered, it will create a image and will register it
		  if(result == null){
			  result = new Image(display, dataByteWidth, dataByteWidth);
			  // create new image
			    GC gc = new GC(result);
			    int bg = byteDataArray.cells[0] % pallet.length;
			    gc.setBackground(pallet[bg]);
			    gc.fillRectangle(result.getBounds());
			    gc.dispose();
		  }
		  return result;
	}
	
	public Image getColumnImage(Object element, int columnIndex) {
		// TODO Auto-generated method stub
		//if (element instanceof ByteDataArray) {
		//	ByteDataArray byteDataArray = (ByteDataArray) element;
		//	if (columnIndex == 17) {
		//		return getByteDataArrayImage(byteDataArray);
		//	}
		//}
		return null;
	}

	public String getColumnText(Object element, int columnIndex) {
		// TODO Auto-generated method stub
		ByteDataArray byteDataArray = (ByteDataArray) element;
		if(columnIndex == 0) {
			return Long.toHexString(byteDataArray.address & 0xFFFFFFFFL);
		}
		return  Integer.toHexString((int)byteDataArray.cells[columnIndex - 1] & 0xFF);
	}

	public Color getBackground(Object element, int columnIndex) {
		// TODO Auto-generated method stub
		if(columnIndex > 0) {
			if (element instanceof ByteDataArray) {
				ByteDataArray byteDataArray = (ByteDataArray) element;
				int index = byteDataArray.cells[columnIndex - 1];
				return pallet[index & 0xFF];
			}
		}
		return null;	
	}

	public Color getForeground(Object element, int columnIndex) {
		// TODO Auto-generated method stub
		return null;
	}

}
